﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebsiteBanSachOnline
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private int sosachtrentrang = 3;
        protected void Page_Load(object sender, EventArgs e)
        {
            BookOnlineDataContext book = new BookOnlineDataContext();
            if (!IsPostBack)
            {
               
                var dsdm = from dm in book.DanhMucs
                           select new { dm.MaDanhMuc, dm.TenDanhMuc };
                bllVerticalMenu.Items.Clear();
                bllVerticalMenu.DisplayMode = BulletedListDisplayMode.HyperLink;
                foreach (var obj in dsdm)
                {
                    ListItem li = new ListItem(obj.TenDanhMuc, "#");
                    bllVerticalMenu.Items.Add(li);
                }
            }
            var books = from b in book.Saches
                        select b;
            int trang = 1;
            if (Request.QueryString["Trang"] != null)
            {
                trang = int.Parse(Request.QueryString["Trang"].ToString());
                showBooks(books.ToList(), trang);
            }
            else
                showBooks(books.ToList(), 1);
            Control ctrl = FindControl("LB_" + trang);
            ////////////////
            ContentPlaceHolder mainContent = (ContentPlaceHolder)this.Master.FindControl("MainContent");
            if (mainContent != null)
            {
                LinkButton lb = (LinkButton)mainContent.FindControl("LB_" + trang);
                if (lb != null) lb.Enabled = false;
            }
        }
        protected void hlTrang_Click(object sender, EventArgs e)
        {
            LinkButton bt = (LinkButton)sender;
            bt.Attributes.Add("font-weight", "bolder");
            bt.Attributes.Add("font-size", "14px");
            bt.Enabled = false;
            int trang = int.Parse(bt.Text);
            Response.Redirect("~/Home.aspx?Trang=" + trang);
          
        }
        public void showBooks(List<Sach> list,int trang) 
        {
           
            Table tb = new Table();
            tb.ID = "Result";
          
            if (list.Count == 0)
            {
                TableRow tr = new TableRow();
                TableCell cell = new TableCell();
                cell.Attributes.Add("style", "text-align:left;padding:10px;font-weight:bold;");
                cell.Text = "Tìm thấy 0 kết quả";
                tr.Cells.Add(cell);
                tb.Rows.Add(tr);
            }
            else
            {
                //khoi tao header;
               
              
                TableRow tr1 = new TableRow();
                TableCell cell1 = new TableCell();
                cell1.Attributes.Add("style", "text-align:left;padding:10px;font-weight:bolder;color:#FF0000;border-bottom:4px solid black;");
                cell1.Text = "Tìm thấy "+list.Count+" kết quả";
                tr1.Cells.Add(cell1);
                tb.Rows.Add(tr1);
                //phan trang


                for (int i = (trang - 1) * sosachtrentrang; i < (trang) * sosachtrentrang; i++) 
                {
                    TableRow tr = new TableRow();
                    int j;
                    for (j = i; j < i + 3 && j < list.Count; j++) 
                    {
                        Sach s = list.ElementAt(j);
                        TableCell cell = new TableCell();
                       
                        cell.Attributes.Add("style", "width:233px;");
                        //do st
                        UCSach ucs = (UCSach)LoadControl("~/UCSach.ascx");
                        ucs.TenSach = s.TenSach;
                        ucs.Gia = ((double)s.GiaBan).ToString("#,##0")+" VND";
                        ucs.LinkHinhAnh = "~/" + s.HinhAnh;
                        ucs.LinkChiTiet = "fsd";
                        ucs.LinkThemVaoGio = "fasdfd";
                        cell.Controls.Add(ucs);
                        tr.Cells.Add(cell);
                    }
                    if (j == trang * sosachtrentrang && j != i + 3)
                    {
                        TableCell cell = new TableCell();
                        cell.Text = "";
                        cell.Attributes.Add("style", "width:auto;");
                        tr.Cells.Add(cell);
                    }
                    tb.Rows.Add(tr);
                    i += 2;
                }
                TableRow tr2 = new TableRow();
                TableCell cell2 = new TableCell();
                cell2.Attributes.Add("style", "border-top:2px solid black;width:100%;");
                int sotrang = (list.Count % sosachtrentrang != 0) ? list.Count/sosachtrentrang + 1 : list.Count()/sosachtrentrang;
                Label lbphantrang = new Label();
                lbphantrang.Attributes.Add("style", "text-align:left;padding-left:10px;float:left;");
                Label text = new Label();
                text.Text = "Trang ";
                lbphantrang.Controls.Add(text);
                for (int i = 1; i < sotrang + 1; i++)
                {
                    LinkButton hl = new LinkButton();
                    hl.Text += i.ToString();
                    hl.ID = "LB_"+i.ToString();
                    hl.Click+=new EventHandler(hlTrang_Click);
                    lbphantrang.Controls.Add(hl);
                    if (i != sotrang) {
                        Label lb1 = new Label();
                        lb1.Text = " | ";
                        lbphantrang.Controls.Add(lb1);
                    }
                    
                }
                cell2.Controls.Add(lbphantrang);
                cell2.ColumnSpan = 3;
                tr2.Cells.Add(cell2);
                tb.Rows.Add(tr2);
            }
            this.right_panel.Controls.Add(tb);
        }

       
    }
}